<?php
class Lottery_winner_model extends BZ_Model
{
    protected $table   = 'lottery_winner';
    protected $table1  = 'express_record';

    /**
     * 查询某抽奖活动的中奖用户数
     * @param unknown $lottery_id
     */
    public function total($params)
    {
        $this->db->select('*');
		$this->db->from($this->table.' AS lottery_winner');
        if ( !empty($params['lottery_id'])) {
            $this->db->where_in('lottery_winner.lottery_id', $params['lottery_id']);
        }
        if ( !empty($params['startDate'])) {
            $this->db->where('lottery_winner.created_at >', $params['startDate'].' 00:00:00');
        }
        if ( !empty($params['endDate'])) {
            $this->db->where('lottery_winner.created_at <=', $params['endDate'].' 23:59:59');
        }
        if ( !empty($params['prize_type'])) {
            $this->db->where('lottery_winner.prize_type', $params['prize_type']);
        }
        if ( !empty($params['user_name'])) {
            $this->db->like('lottery_winner.user_name', $params['user_name'], 'after');
        }
        if ( !empty($params['status'])) {
            $this->db->where('lottery_winner.status', $params['status']);
        }
        if ( !empty($params['prize_type'])) {
            $this->db->where('lottery_winner.prize_type', $params['prize_type']);
        }
        if ( !empty($params['grade'])) {
            $this->db->where('lottery_winner.grade', $params['grade']);
        }

        if ( !empty($params['reseller_name'])) {
            $this->db->like('lottery_winner.reseller_name', $params['reseller_name'], 'after');
        }

        return $this->db->count_all_results();
    }

	/**
     * 查询某抽奖活动的所有中奖用户
	 * @param number $page_num
	 * @param number $num
     * @param unknown $lottery_id
     */
    public function page_list($page_num, $num, $params)
    {
		$this->db->select('lottery_winner.*');
		$this->db->from($this->table.' AS lottery_winner');
        if ( !empty($params['lottery_id'])) {
            $this->db->where_in('lottery_winner.lottery_id', $params['lottery_id']);
        }
        if ( !empty($params['startDate'])) {
            $this->db->where('lottery_winner.created_at >', $params['startDate'].' 00:00:00');
        }
        if ( !empty($params['endDate'])) {
            $this->db->where('lottery_winner.created_at <=', $params['endDate'].' 23:59:59');
        }
        if ( !empty($params['prize_type'])) {
            $this->db->where('lottery_winner.prize_type', $params['prize_type']);
        }
        if ( !empty($params['user_name'])) {
            $this->db->like('lottery_winner.user_name', $params['user_name'], 'after');
        }

        if ( !empty($params['status'])) {
            $this->db->where('lottery_winner.status', $params['status']);
        }
        if ( !empty($params['prize_type'])) {
            $this->db->where('lottery_winner.prize_type', $params['prize_type']);
        }
        if ( !empty($params['grade'])) {
            $this->db->where('lottery_winner.grade', $params['grade']);
        }

        if ( !empty($params['reseller_name'])) {
            $this->db->like('lottery_winner.reseller_name', $params['reseller_name'], 'after');
        }

        $this->db->order_by('lottery_winner.id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /**
     * 根据id 查询
     * @param $id
     * @return mixed
     */
    public function findById($id)
    {
        $this->db->select('lottery_winner.id,status,prize_type,address,created_at,updated_at, prize_name,uid, express_num, user_name ,effective_date, express_record.name, express_record.number,express_record.context');
        $this->db->from($this->table.' AS lottery_winner');
        $this->db->join($this->table1.' AS express_record', 'express_record.number=lottery_winner.express_num', 'LEFT');
        $this->db->where('lottery_winner.id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 更新状态  处理完成
     * @param $id
     * @return mixed
     */
    public function updateStatus($id)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table, array('status' => 3));
    }

    /**
     * 更新
     * @param $id
     * @param $data
     * @return mixed
     */
    public function updateWinner($id, $data)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table, $data);
    }

    /**
     * 查找一位 根据id
     */
    public function findOneWinner($id)
    {
        $this->db->select('lottery_winner.id,status,prize_type,address,created_at,updated_at, prize_name,uid, express_num, user_name ,effective_date');
        $this->db->from($this->table);
        $this->db->where('lottery_winner.id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }
}